package translationModels;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;

public class BidirectedModel implements IBMModel
{
	private IBMModel etof;
	private IBMModel ftoe;
	
	public BidirectedModel(IBMModel etof, IBMModel ftoe)
	{
		this.etof = etof;
		this.ftoe = ftoe;
	}

	public HashMap<TParam, Double> learnParams(TParamsInitialization initMethod, int iterations) 
	{
		HashMap<TParam, Double> ret = ftoe.learnParams(initMethod, iterations);
		etof.learnParams(initMethod, iterations);
		return ret;
	}

	public ArrayList<HashMap<Integer, Integer>> calculateAlignments() 
	{
		ArrayList<HashMap<Integer, Integer>> alignmentsftoe = ftoe.calculateAlignments();
		ArrayList<HashMap<Integer, Integer>> alignmentsetof = etof.calculateAlignments();
		ArrayList<HashMap<Integer, Integer>> alignments = new ArrayList<HashMap<Integer, Integer>>();
		for(int i = 0; i < alignmentsetof.size(); i++)
		{
			alignments.add(new HashMap<Integer, Integer>());
			for(Entry<Integer,Integer> entry : alignmentsftoe.get(i).entrySet())
			{
				if(alignmentsetof.get(i).get(entry.getValue()) == entry.getKey())
					alignments.get(i).put(entry.getKey(), entry.getValue());
				if(!alignmentsetof.get(i).containsValue(entry.getKey()))
					alignments.get(i).put(entry.getKey(), entry.getValue());
			}
			for(Entry<Integer,Integer> entry : alignmentsetof.get(i).entrySet())
			{
				if(!alignmentsftoe.get(i).containsKey(entry.getValue()))
					alignments.get(i).put(entry.getValue(), entry.getKey());
			}
		}
		return alignments;
	}
}
